library(SDMTools)
wd='/home/jc148322/rob/';setwd(wd)

#read in the asciis
bdall=read.asc('Bdall.asc')
bdwet=read.asc('Bdwet.asc')
bc12=read.asc('bc12.asc')
bc01=read.asc('bc01.asc')
veg=read.asc('veg.asc')


######################################
#create the data
pos = as.data.frame(which(is.finite(bdall), arr.ind=TRUE))
pos$lat = getXYcoords(bdall)$y[pos$col]; pos$lon =
getXYcoords(bdall)$x[pos$row] #append the lat lon
pos$bdall= extract.data(cbind(pos$lon,pos$lat),bdall)   #extract the data
pos$bdwet= extract.data(cbind(pos$lon,pos$lat),bdwet)
pos$bc01= extract.data(cbind(pos$lon,pos$lat),bc01)
pos$bc12 = extract.data(cbind(pos$lon,pos$lat),bc12)
pos$veg = extract.data(cbind(pos$lon,pos$lat),veg)

#read in the threshold to create sigdiff
threshold=read.csv('maxentResults.csv')
threshold=threshold$Minimum.training.presence.logistic.threshold
bdall[which(bdall<threshold[1])]=0; #set values below threshold to 0
bdwet[which(bdwet<threshold[2])]=0; #set values below threshold to 0
sig.diff=SigDiff(bdall,bdwet)
pos$sig.diff=extract.data(cbind(pos$lon,pos$lat),sig.diff)
write.csv(pos,'coord.csv')
